package com.steam.service.impl;

import com.steam.domain.Product;
import com.steam.domain.Store;
import com.steam.dto.ProductDto;
import com.steam.feign.StoreService;
import com.steam.mapper.ProductMapper;
import com.steam.service.IProductService;
import com.steam.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private StoreService storeService;

    @Override
    @Transactional
    public JsonResult add(ProductDto productDto) {
        try {
            Product product = new Product();
            product.setName(productDto.getName());
            product.setPrice(productDto.getPrice());
            productMapper.add(product);

            Store store = new Store();
            store.setProductId(product.getId());
            store.setNumber(productDto.getNumber());
            JsonResult add = storeService.add(store);
            if (!add.isSuccess()){
                return add;
            }
            return new JsonResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败，请重试！");
        }
    }
}
